package base.validation.constraint;

import base.util.BeanUtil;
import base.validation.annotation.Uniqueness;
import lombok.extern.slf4j.Slf4j;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * Uniqueness validator
 *
 * @description: 唯一校验实现
 * @author: cyj
 * @date 2020 -10-19 18:47:43
 */
@Slf4j
public class UniquenessValidator implements ConstraintValidator<Uniqueness, Object> {

    private Class<?> classType;

    private String methodName;

    @Override
    public void initialize(Uniqueness constraintAnnotation) {
        this.classType = constraintAnnotation.classType();
        this.methodName = constraintAnnotation.method();
    }

    @Override
    public boolean isValid(Object value, ConstraintValidatorContext constraintValidatorContext) {
        return null == BeanUtil.springInvoke(classType, methodName, value);
    }
}
